# coding=utf-8
# author:xc
# datetime:2018/8/3 16:04
from . import api
from flask import request, jsonify
import create_order

from app import util, redis

from app import util
# return_url = "http://5ic.me/api/wechat/callback"
return_url = ['http://gz.deyiauto.com/api/wechat/callback', 'http://gz.deyiauto.com/api/wechat/pay', 'http://gz.deyiauto.com/api/wechat/business']


# 微信支付调起接口
@api.route('/wechat', methods=['POST','GET'])
def api_pay_vip():
    order_num = request.values.get('order_num')  # 订单编号
    money = request.values.get('money')  # 支付金额单位分
    openId = request.values.get('openId')  # 用户标识
    plateNum = request.values.get('plateNum', default="0")
    machineNum = request.values.get("machineNum", '')
    userflag = request.values.get('userflag', 0)  # 接口标识 0 用户下单 1 商家下单  2 商家充值
    print userflag
    print order_num
    print machineNum,order_num,plateNum
    if machineNum is None or machineNum == "":
        machineId = 0
    else:
        res = redis.hget("all_machine_conf", machineNum)
        print res
        if res is None:
            machineId =0
        else:
            res = eval(res)
            machineId = res['machineId']
    client_ip = request.headers.get('X-Real-Ip', request.remote_addr)
    data = create_order.client_send(return_url[int(userflag)], order_num, client_ip, money, openId, plateNum, machineId)
    return util.to_json(data)


# 微信支付回调接口
@api.route('/api/wechat/callback', methods=['GET', 'POST'])
def test_xml():
    data = request.data
    res = create_order.api_wx_return(data)
    print res
    return res


# 商家微信支付回调接口
@api.route('/api/wechat/pay', methods=['GET', 'POST'])
def wechat_pay():
    data = request.data
    res = create_order.api_wx_business_return(data)
    print res
    return res


# 商家充值回调接口
@api.route('/api/wechat/business', methods=['GET', 'POST'])
def wechat_business():
    data = request.data
    res = create_order.api_wx_business_recharge(data)
    print res
    return res
